using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Collections.Generic;
using ASPNETVN.PORTAL.Components.Helpers;

namespace ASPNETVN.PORTAL.Modules.TAB
{
    public partial class Tabs : PortalModuleControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                this.TabDataBind();
            }
        }

        private void TabDataBind()
        {
            Components.Configuration.Items.Portal portal = this.Portal;
            foreach (Tab parentTab in portal.Tabs.Where(x => x.ParentID == Guid.Empty).OrderBy(x => x.SortOrder))
            {
                tabList.Items.Add(new ListItem(parentTab.Name, parentTab.ID.ToString()));
                foreach (Tab c1 in portal.Tabs.Where(x => x.ParentID == parentTab.ID).OrderBy(x => x.SortOrder))
                {
                    tabList.Items.Add(new ListItem("-- " + c1.Name, c1.ID.ToString()));
                    foreach (Tab c2 in portal.Tabs.Where(x => x.ParentID == c1.ID).OrderBy(x => x.SortOrder))
                    {
                        tabList.Items.Add(new ListItem("---- " + c2.Name, c2.ID.ToString()));
                    }
                }
            }
        }

        protected void UpDown_Click(Object sender, ImageClickEventArgs e)
        {
            string cmd = ((ImageButton) sender).CommandName;            
            if (tabList.SelectedIndex > -1)
            {
                int index = tabList.SelectedIndex;
                if (cmd == "down")
                {
                    if (index < tabList.Items.Count - 1)
                    {

                        Tab t1 = (Tab)this.Portal.Tabs.First(x => x.ID == new Guid(tabList.Items[index].Value));
                        Tab t2 = (Tab)this.Portal.Tabs.First(x => x.ID == new Guid(tabList.Items[index + 1].Value));
                        this.SwapTabSortOrder(t1, t2);
                    }
                }
                else if (cmd == "up")
                {
                    if (index > 0)
                    {
                        Tab t1 = (Tab)this.Portal.Tabs.First(x => x.ID == new Guid(tabList.Items[index].Value));
                        Tab t2 = (Tab)this.Portal.Tabs.First(x => x.ID == new Guid(tabList.Items[index - 1].Value));
                        this.SwapTabSortOrder(t1, t2);
                    }
                }
                
                // Redirect to this site to refresh
                Response.Redirect(Request.RawUrl);
            }
        }

        private void SwapTabSortOrder(Tab t1, Tab t2)
        {
            PortalConfiguration configuration = new PortalConfiguration();
            configuration.SwapTabSortOrder(t1, t2);
        }

        //*******************************************************
        //
        // The DeleteBtn_Click server event handler is used to delete
        // the selected tab from the portal
        //
        //*******************************************************

        protected void DeleteBtn_Click(Object sender, ImageClickEventArgs e)
        {
            if (tabList.SelectedIndex != -1)
            {
                // must delete from database too
                Tab t = this.Portal.Tabs.First(x => x.ID == new Guid(tabList.SelectedValue));
                PortalConfiguration config = new PortalConfiguration();
                config.DeleteTab(t.ID);

                // remove item from list
                this.Portal.Tabs.Remove(t);

                // reorder list
                OrderTabs();

                // Redirect to this site to refresh
                Response.Redirect(Request.RawUrl);                
            }
        }


        //*******************************************************
        //
        // The AddTab_Click server event handler is used to add
        // a new security tab for this portal
        //
        //*******************************************************

        protected void AddTab_Click(Object Sender, EventArgs e)
        {
            // Obtain Portal from Current Context
            Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal) Context.Items["Portal"];

            // New tabs go to the end of the list
            Tab tab = new Tab();
            tab.Name = "[New Tab]";
            tab.ID = Guid.NewGuid();
            tab.ParentID = Guid.Empty;
            tab.PortalID = Global.PortalID;
            tab.SortOrder = 9999;

            // All users.
            tab.AuthorizedRoles = "-1;";
            tab.ShowLeftPane = true;
            tab.ShowRightPane = true;
            tab.ThemeName = Global.GetThemeName();
            tab.ShowOnMenu = true;
            
            this.Portal.Tabs.Add(tab);

            // Write tab to database.
            PortalConfiguration config = new PortalConfiguration();
            tab.ID = config.AddTab(tab);

            // Reload the _portal from the database
            HttpContext.Current.Items["Portal"] = new Components.Configuration.Items.Portal(tab.ID);

            // Reset the order numbers for the tabs within the list  
            OrderTabs();

            // Redirect to edit page
            //string url = string.Format("~/Default.aspx?tabid={0}&mid={1}&aecid=AddEditTab&returnUrl={2}", tab.ID, this.ModuleID, Server.UrlEncode(Request.RawUrl));
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditTab", "tid={0}", tab.ID);
            Response.Redirect(url);
        }

        //*******************************************************
        //
        // The EditBtn_Click server event handler is used to edit
        // the selected tab within the portal
        //
        //*******************************************************

        protected void EditBtn_Click(Object sender, ImageClickEventArgs e)
        {
            // Redirect to edit page of currently selected tab
            if (tabList.SelectedIndex != -1)
            {
                // Redirect to module settings page
                Tab tab = this.Portal.Tabs.First(x => x.ID == new Guid(tabList.SelectedValue));
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditTab", "tid={0}", tab.ID);
                Response.Redirect(url);
            }
        }

        protected void OrderTabs()
        {
            ////this.Portal.Tabs.Sort();
            //int i = 1;
            //foreach (Tab t in this.Portal.Tabs)
            //{
            //    t.SortOrder = i;
            //    i += 2;
            //    PortalConfiguration config = new PortalConfiguration();
            //    config.UpdateTabOrder(t.ID, t.SortOrder);
            //}
        }
    }
}